package com.biodiv.server.handler;


import com.biodiv.common.instance.ServiceInstance;
import com.biodiv.common.message.PullServiceRequestMessage;
import com.biodiv.common.message.PullServiceResponseMessage;
import com.biodiv.server.registry.ServerInstanceManager;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.Set;

/**
 * @Author: tianquan
 * @date: 2024-11-03  17:08
 * @Description: 拉取服务实例
 */
@Slf4j
@ChannelHandler.Sharable
public class PullServiceRequestMessageHandler extends SimpleChannelInboundHandler<PullServiceRequestMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, PullServiceRequestMessage pullServiceRequestMessage) throws Exception {
        // 返回所有实例对象
        Map<String, Set<ServiceInstance>> instances = ServerInstanceManager.getInstances();

        log.info("-----------------客户端拉取服务实例------------------");
        instances.forEach(((k, v) -> log.info("服务名: {}, 实例数量: {}", k, v.size())));

        PullServiceResponseMessage pullServiceResponseMessage = new PullServiceResponseMessage(true, null, instances);
        channelHandlerContext.writeAndFlush(pullServiceResponseMessage);
    }

}
